package com.kakavr.game.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Maps;
import com.kakavr.auth.util.EncryptionUtils;
import com.kakavr.base.controller.BaseController;
import com.kakavr.base.exception.ValidateParamException;
import com.kakavr.game.service.IShopLoginService;
import com.kakavr.result.MsgResult;
import com.kakavr.result.constant.MsgConstants;
import com.kakavr.util.SessionUtils;
/**
 * 
 * @author Administrator
 *游戏机登录
 */
@RestController
@RequestMapping("/n/shopLogin")
public class ShopLoginController extends BaseController {
	@Resource
	private IShopLoginService shopLoginService;
	
	/**
	 * 店铺登录
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "signIn", method = RequestMethod.POST)
	public Map<String, Object> signIn(HttpServletRequest request){
		String userName = request.getParameter("code");
		String password = request.getParameter("password");
		if(StringUtils.isBlank(userName)){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001, MsgConstants.MSG_CONTENT_1001,"用户名");
		}
		if(StringUtils.isBlank(password)){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001, MsgConstants.MSG_CONTENT_1001,"密码");
		}
		Map<String,Object> map = Maps.newHashMap();
		map.put("code", userName);
		Map<String,Object> shop = shopLoginService.selectShopInfo(map);
		if(null == shop || shop.isEmpty()){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002, MsgConstants.MSG_CONTENT_1002,"用户名密码");
		}
		Object pass = shop.get("password");
		if(password.equals(pass) || EncryptionUtils.md5(password).equals(pass)){
			SessionUtils.setCurUser(shop);
			map.clear();
			map.put("storeId", shop.get("id"));
			map.put("typeId", shop.get("typeId"));
			return MsgResult.okResultData(map);
		}
		throw new ValidateParamException(MsgConstants.MSG_CODE_1002, MsgConstants.MSG_CONTENT_1002,"用户名密码");
	}

}
